import Vue from "vue";
import VueRouter from "vue-router";
import store from "./store";
import { setDocumentTitle } from "./utils/dom";
import { isLogin } from "./utils/auth";

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err);
};

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    component: require("./layouts/BasicLayout.vue").default,
    children: [
      {
        path: "/",
        redirect: "/home"
      },
      {
        path: "/home",

        component: require("./views/Home/Home.vue").default,
        children: [
          {
            path: "",
            name: "Home",
            meta: {
              beforeClose: false,
              title: "首页",
              login: false,
              auth: false
            },
            component: () =>
              import(/* webpackChunkName: "Home" */ "./views/Home/HomeMain.vue")
          },
          {
            path: "/trade",
            name: "Trade",
            meta: {
              beforeClose: false,
              title: "交易说明",
              login: false,
              auth: false
            },
            component: () =>
              import(/* webpackChunkName: "Trade" */ "./views/Home/Trade.vue")
          },
          {
            path: "/new-company",
            name: "NewCompany",
            meta: {
              beforeClose: false,
              title: "企业列表",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "NewCompany" */ "./views/Home/NewCompany.vue"
              )
          },
          {
            path: "/new-public",
            name: "NewPublic",
            meta: {
              beforeClose: false,
              title: "最新发布",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "NewCompany" */ "./views/Home/NewPublic.vue"
              )
          },
          {
            path: "/help",
            name: "Help",
            meta: {
              beforeClose: false,
              title: "帮助中心",
              login: false,
              auth: false
            },
            component: () =>
              import(/* webpackChunkName: "Help" */ "./views/Home/Help.vue")
          }
        ]
      },
      {
        path: "/user-center",
        component: require("./views/UserCenter/UserCenter.vue").default,
        redirect: "/user-center/profile",
        children: [
          {
            name: "UserCenterProfile",
            path: "/user-center/profile",
            meta: {
              beforeClose: false,
              title: "用户中心",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "UserCenter" */ "./views/UserCenter/Profile.vue"
              )
          },
          {
            name: "UserCenterGoods",
            path: "/user-center/goods",
            meta: {
              beforeClose: false,
              title: "用户中心",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "UserCenter" */ "./views/UserCenter/Goods.vue"
              )
          },
          {
            name: "UserCenterProject",
            path: "/user-center/project",
            meta: {
              beforeClose: false,
              title: "用户中心",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "UserCenter" */ "./views/UserCenter/Project.vue"
              )
          },
          {
            name: "UserCenterStoreEdit",
            path: "/user-center/store-edit",
            meta: {
              beforeClose: false,
              title: "用户中心",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "UserCenter" */ "./views/UserCenter/StoreEdit.vue"
              )
          },
          {
            name: "UserCenterGoodsEdit",
            path: "/user-center/goods-edit",
            meta: {
              beforeClose: false,
              title: "用户中心",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "UserCenter" */ "./views/UserCenter/GoodsEdit.vue"
              )
          },
          {
            name: "UserCenterOrders",
            path: "/user-center/orders",
            meta: {
              beforeClose: false,
              title: "用户中心",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "UserCenter" */ "./views/UserCenter/Orders.vue"
              )
          },
          {
            name: "UserCenterOrdersEdit",
            path: "/user-center/orders-edit",
            meta: {
              beforeClose: false,
              title: "用户中心",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "UserCenter" */ "./views/UserCenter/OrdersEdit.vue"
              )
          },
          {
            name: "UserCenterSeller",
            path: "/user-center/seller",
            meta: {
              beforeClose: false,
              title: "用户中心",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "UserCenter" */ "./views/UserCenter/Seller.vue"
              )
          },
          {
            name: "UserCenterSecurity",
            path: "/user-center/security",
            meta: {
              beforeClose: false,
              title: "用户中心",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "UserCenter" */ "./views/UserCenter/Security.vue"
              )
          }
        ]
      },
      {
        path: "/business",
        name: "Business",
        meta: {
          beforeClose: false,
          title: "供应",
          login: false,
          auth: false
        },
        component: () =>
          import(
            /* webpackChunkName: "Business" */ "./views/Business/Business.vue"
          )
      },
      {
        path: "/orders",
        name: "Orders",
        meta: {
          beforeClose: false,
          title: "求购",
          login: false,
          auth: false
        },
        component: () =>
          import(/* webpackChunkName: "Orders" */ "./views/Orders/Orders.vue")
      },
      {
        path: "/orders/:id",
        name: "OrdersPreview",
        meta: {
          beforeClose: false,
          title: "求购",
          login: false,
          auth: false
        },
        component: () =>
          import(
            /* webpackChunkName: "Orders" */ "./views/Orders/OrdersPreview.vue"
          )
      },
      {
        path: "/info",
        component: require("./views/Info/Info.vue").default,
        children: [
          {
            path: "",
            name: "Info",
            meta: {
              beforeClose: false,
              title: "目录列表",
              login: false,
              auth: false
            },
            component: () =>
              import(/* webpackChunkName: "Info" */ "./views/Info/InfoList.vue")
          },
          {
            path: ":id",
            name: "InfoDetail",
            meta: {
              beforeClose: false,
              title: "行业资讯",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "Info" */ "./views/Info/InfoDetail.vue"
              )
          }
        ]
      },
      {
        path: "/user/register/success",
        name: "RegisterSuccess",
        meta: {
          beforeClose: false,
          title: "注册成功",
          login: false,
          auth: false
        },
        component: () =>
          import(
            /* webpackChunkName: "RegisterSuccess" */ "./views/User/RegisterSuccess.vue"
          )
      },
      {
        path: "/user/auth/step1",
        name: "UserAuthStep1",
        meta: {
          beforeClose: false,
          title: "认证会员",
          login: false,
          auth: false
        },
        component: () =>
          import(
            /* webpackChunkName: "UserAuthStep1" */ "./views/UserAuth/UserAuthStep1.vue"
          )
      },
      {
        path: "/user/auth/step2",
        name: "UserAuthStep2",
        meta: {
          beforeClose: false,
          title: "认证会员",
          login: false,
          auth: false
        },
        component: () =>
          import(
            /* webpackChunkName: "UserAuthStep2" */ "./views/UserAuth/UserAuthStep2.vue"
          )
      },
      {
        path: "/user/auth/step3",
        name: "UserAuthStep3",
        meta: {
          beforeClose: false,
          title: "认证会员",
          login: false,
          auth: false
        },
        component: () =>
          import(
            /* webpackChunkName: "UserAuthStep3" */ "./views/UserAuth/UserAuthStep3.vue"
          )
      },
      {
        path: "/user/auth/step4",
        name: "UserAuthStep4",
        meta: {
          beforeClose: false,
          title: "认证会员",
          login: false,
          auth: false
        },
        component: () =>
          import(
            /* webpackChunkName: "UserAuthStep4" */ "./views/UserAuth/UserAuthStep4.vue"
          )
      },
      {
        path: "/project",
        name: "Project",
        meta: {
          beforeClose: false,
          title: "项目首页",
          login: false,
          auth: false
        },
        component: () =>
          import(
            /* webpackChunkName: "Project" */ "./views/Project/Project.vue"
          )
      },
      {
        path: "/project/apply/step1",
        name: "ProjectApplyStep1",
        meta: {
          beforeClose: false,
          title: "项目信用申请",
          login: false,
          auth: false
        },
        component: () =>
          import(
            /* webpackChunkName: "ProjectApplyStep1" */ "./views/Project/ProjectApplyStep1.vue"
          )
      },
      {
        path: "/project/apply/step2",
        name: "ProjectApplyStep2",
        meta: {
          beforeClose: false,
          title: "项目信用申请",
          login: false,
          auth: false
        },
        component: () =>
          import(
            /* webpackChunkName: "ProjectApplyStep2" */ "./views/Project/ProjectApplyStep2.vue"
          )
      },
      {
        path: "/project/apply/step3",
        name: "ProjectApplyStep3",
        meta: {
          beforeClose: false,
          title: "项目信用申请",
          login: true,
          auth: false
        },
        component: () =>
          import(
            /* webpackChunkName: "ProjectApplyStep3" */ "./views/Project/ProjectApplyStep3.vue"
          )
      },
      {
        path: "/project/apply/step4",
        name: "ProjectApplyStep4",
        meta: {
          beforeClose: false,
          title: "项目信用申请",
          login: false,
          auth: false
        },
        component: () =>
          import(
            /* webpackChunkName: "ProjectApplyStep4" */ "./views/Project/ProjectApplyStep4.vue"
          )
      },
      {
        path: "/project/:id",
        name: "ProjectDetail",
        meta: {
          beforeClose: false,
          title: "项目信用详情",
          login: false,
          auth: false
        },
        component: () =>
          import(
            /* webpackChunkName: "ProjectDetail" */ "./views/Project/ProjectDetail.vue"
          )
      },
      {
        path: "/company",
        component: require("./views/Company/Company.vue").default,
        children: [
          {
            name: "CompanyHome",
            path: "/company/:id",
            meta: {
              beforeClose: false,
              title: "商家首页",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "Company" */ "./views/Company/CompanyHome.vue"
              )
          },
          {
            name: "GoodsList",
            path: "/company/:id/goods",
            meta: {
              beforeClose: false,
              title: "商家首页",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "Company" */ "./views/Company/GoodsList.vue"
              )
          },
          {
            name: "GoodsDetail",
            path: "/company/:id/goods/:goods",
            meta: {
              beforeClose: false,
              title: "商家首页",
              login: false,
              auth: false
            },
            component: () =>
              import(
                /* webpackChunkName: "Company" */ "./views/Company/GoodsDetail.vue"
              )
          }
        ]
      },
      {
        path: "/static",
        name: "Static",
        meta: {
          beforeClose: false,
          title: "用户须知",
          login: false,
          auth: false
        },
        component: () =>
          import(/* webpackChunkName: "Static" */ "./views/Static/Static")
      }
    ]
  },

  {
    path: "/user",
    hideInMenu: true,
    component: () =>
      import(/* webpackChunkName: "layout" */ "./layouts/UserLayout"),
    children: [
      {
        path: "/user",
        redirect: "/user/login"
      },
      {
        path: "/user/login",
        name: "login",
        meta: {
          beforeClose: false,
          title: "登录",
          login: false,
          auth: false
        },
        component: () =>
          import(/* webpackChunkName: "user" */ "./views/User/Login")
      },
      {
        path: "/user/register",
        name: "register",
        meta: {
          beforeClose: false,
          title: "注册",
          login: false,
          auth: false
        },
        component: () => import(/* ChunkName: "user" */ "./views/User/Register")
      }
    ]
  }
];

const router = new VueRouter({
  scrollBehavior(to, from, pos) {
    return pos || { x: 0, y: 0 };
  },
  mode: process.env.ENV === "dev" ? "hash" : "history",
  routes
});

router.beforeEach(async (to, from, next) => {
  if (Object.keys(store.getters.account).length === 0 && isLogin()) {
    await store.dispatch("getAccountAction");
  }

  if (Object.keys(store.getters.config).length === 0) {
    await store.dispatch("getConfig");
  }

  setDocumentTitle(
    to.meta.title +
      "-" +
      (store.getters.config.p_name || "厦门市建筑材料供应链综合服务平台")
  );

  if (from.meta.beforeClose && window.IS_EDIT) {
    Vue.prototype.$confirm({
      content: "您有未保存的数据，确认要离开吗",
      onOk() {
        next();
      },
      cancelText: "取消",
      confirmText: "确定"
    });
  } else {
    next();
  }
});

router.afterEach(() => {
  window.IS_EDIT = false;
});

export default router;
